package com.ysxsoft.wdg.ui.activity.my;

import android.content.Context;
import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.classic.common.MultipleStatusView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnLoadmoreListener;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;
import com.ysxsoft.wdg.R;
import com.ysxsoft.wdg.adapter.my.CollectGoodsAdapter;
import com.ysxsoft.wdg.adapter.my.CollectShopAdapter;
import com.ysxsoft.wdg.base.BaseActivity;
import com.ysxsoft.wdg.bean.BaseBean;
import com.ysxsoft.wdg.bean.response.CollectProductResponse;
import com.ysxsoft.wdg.bean.response.CollectShopResponse;
import com.ysxsoft.wdg.bean.response.CollectStoreResponse;
import com.ysxsoft.wdg.bean.response.PinOrderResponse;
import com.ysxsoft.wdg.net.CallbackCode;
import com.ysxsoft.wdg.net.UrlHost;
import com.ysxsoft.wdg.ui.activity.center_st.StShopDetailActivity;
import com.ysxsoft.wdg.ui.activity.center_zy.GoodsDetailActivity;
import com.ysxsoft.wdg.util.JsonUtils;
import com.ysxsoft.wdg.util.ToastUtils;
import com.ysxsoft.wdg.util.sp.SharePrefUtils;
import com.ysxsoft.wdg.util.transform.DividerGridItemDecoration;
import com.ysxsoft.wdg.widget.SlideRecyclerView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import okhttp3.Call;

public class CollectionActivity extends BaseActivity implements OnRefreshListener, OnLoadmoreListener {

    public static void start(Context context) {
        Intent intent = new Intent(context, CollectionActivity.class);
        context.startActivity(intent);
    }


    @BindView(R.id.topView)
    View topView;
    @BindView(R.id.title_finish)
    ImageView titleFinish;
    @BindView(R.id.title_content)
    TextView titleContent;
    @BindView(R.id.tv_goods)
    TextView tvGoods;
    @BindView(R.id.tv_shop)
    TextView tvShop;
    @BindView(R.id.recyclerview)
    SlideRecyclerView recyclerview;
    @BindView(R.id.recyclerview2)
    SlideRecyclerView recyclerview2;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;
    @BindView(R.id.multStatusView)
    MultipleStatusView multStatusView;

    private CollectGoodsAdapter goodsAdapter;
    private CollectShopAdapter shopAdapter;

    private int page = 1;
    private boolean isProductSelected = true;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_collection;
    }

    @Override
    protected void initData() {
        initStatusBar(topView, false);
        titleContent.setText("我的收藏");
        tvGoods.setSelected(true);

        recyclerview.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        goodsAdapter = new CollectGoodsAdapter(R.layout.item_collect_goods);
        recyclerview.setAdapter(goodsAdapter);
        recyclerview.addItemDecoration(new DividerGridItemDecoration(this, 2, R.color.colorBackground));


        recyclerview2.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.VERTICAL, false));
        shopAdapter = new CollectShopAdapter(R.layout.item_collect_shop);
        recyclerview2.setAdapter(shopAdapter);
        recyclerview2.addItemDecoration(new DividerGridItemDecoration(this, 2, R.color.colorBackground));
//        shopAdapter.setNewData(list);

        getCollectProductList();
    }

    @Override
    protected void setListener() {
        goodsAdapter.setOnDeleteClickListener(new CollectGoodsAdapter.OnDeleteClickLister() {
            @Override
            public void onDeleteClick(View view, int position) {
                recyclerview.closeMenu();
                CollectProductResponse.DataBean response=goodsAdapter.getItem(position);
                goodsAdapter.remove(position);
                cancelGoods(""+response.getProductid());//取消收藏商品id
            }
        });

        shopAdapter.setOnDeleteClickListener(new CollectShopAdapter.OnDeleteClickLister() {
            @Override
            public void onDeleteClick(View view, int position) {
                recyclerview2.closeMenu();
                CollectStoreResponse.DataBean response=shopAdapter.getItem(position);
                shopAdapter.remove(position);

                collectShop(""+response.getCompanyid());//取消收藏商品id
            }
        });

        goodsAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CollectProductResponse.DataBean item= (CollectProductResponse.DataBean) adapter.getItem(position);
                GoodsDetailActivity.start(mContext,""+item.getProductid());
            }
        });

        shopAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                CollectStoreResponse.DataBean d= (CollectStoreResponse.DataBean) adapter.getItem(position);
                StShopDetailActivity.start(mContext,d.getCompanyid()+"");
            }
        });

        refreshLayout.setOnRefreshListener(this);
        refreshLayout.setOnLoadmoreListener(this);
    }


    @OnClick({R.id.title_finish, R.id.tv_goods, R.id.tv_shop})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.title_finish:
                finish();
                break;
            case R.id.tv_goods:
                isProductSelected = true;
                tvShop.setSelected(false);
                tvGoods.setSelected(true);
                recyclerview.setVisibility(View.VISIBLE);
                recyclerview2.setVisibility(View.GONE);
                page = 1;
                getCollectProductList();
                break;
            case R.id.tv_shop:
                isProductSelected = false;
                tvGoods.setSelected(false);
                tvShop.setSelected(true);
                recyclerview.setVisibility(View.GONE);
                recyclerview2.setVisibility(View.VISIBLE);
                page = 1;
                getCollectStoreList();
                break;
        }
    }

    private void reset() {
        if (refreshLayout.isRefreshing()) {
            refreshLayout.finishRefresh(true);
        }
        refreshLayout.finishLoadmore();
    }

    private void getCollectProductList() {
        OkHttpUtils.post()
                .url(UrlHost.COLLECT_PRODUCT_LIST)
//                .addParams("uid", uid)
                .addParams("uid", SharePrefUtils.getUserId())
                .addParams("page", page + "")
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                    multStatusView.showError();
                    reset();
                }
            }

            @Override
            public void onResponse(String response, int id) {
                reset();
                if (response != null) {
                    CollectProductResponse pinOrderResponse = JsonUtils.parseByGson(response, CollectProductResponse.class);
                    if (pinOrderResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(pinOrderResponse.getCode())) {
                        List<CollectProductResponse.DataBean> list = pinOrderResponse.getData();
                        if (page == 1) {
                            if (list.isEmpty()) {
                                multStatusView.showEmpty();
                            } else {
                                multStatusView.showContent();
                            }
                            goodsAdapter.setNewData(list);
                        } else {
                            goodsAdapter.addData(list);
                        }
                    }
                } else {
                    multStatusView.showError();
                }
            }
        });
    }

    private void getCollectStoreList() {
        OkHttpUtils.post()
                .url(UrlHost.COLLECT_STORE_LIST)
                .addParams("uid", SharePrefUtils.getUserId())
                .addParams("page", page + "")
                .addParams("lat", SharePrefUtils.getLatitude())
                .addParams("lng", SharePrefUtils.getLongitude())
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                    reset();
                }
            }

            @Override
            public void onResponse(String response, int id) {
                reset();
                if (response != null) {
                    CollectStoreResponse collectStoreResponse = JsonUtils.parseByGson(response, CollectStoreResponse.class);
                    if (collectStoreResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(collectStoreResponse.getCode())) {
                        List<CollectStoreResponse.DataBean> list = collectStoreResponse.getData();
                        if (page == 1) {
                            if (list.isEmpty()) {
                                multStatusView.showEmpty();
                            } else {
                                multStatusView.showContent();
                            }
                            shopAdapter.setNewData(list);
                        } else {
                            shopAdapter.addData(list);
                        }
                    } else {
                        if (page == 1) {
                            multStatusView.showEmpty();
                        }
                        ToastUtils.showToast(collectStoreResponse.getMs());
                    }
                } else {
                    multStatusView.showError();
                }
            }
        });
    }

    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        page++;
        if (isProductSelected) {
            getCollectProductList();
        } else {
            getCollectStoreList();
        }
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        page = 1;
        if (isProductSelected) {
            getCollectProductList();
        } else {
            getCollectStoreList();
        }
    }


    private void cancelGoods(String goodsid){
        mApiHelper.colZyGoods(goodsid, SharePrefUtils.getUserId(), new Observer<BaseBean>() {
            @Override
            public void onSubscribe(Disposable d) {

            }

            @Override
            public void onNext(BaseBean baseBean) {
                ToastUtils.showToast(baseBean.getMs());
                if (baseBean.getCode().equals(CallbackCode.SUCCESS)) {
                    ToastUtils.showToast(baseBean.getMs());
                }
            }

            @Override
            public void onError(Throwable e) {
                ToastUtils.showToast("请稍后再试");
            }

            @Override
            public void onComplete() {

            }
        });
    }
    /**
     * 取消收藏店铺
     */
    private void collectShop(String shopId) {
        OkHttpUtils.post()
                .url(UrlHost.COLLECT_SHOP)
                .addParams("uid", SharePrefUtils.getUserId())
                .addParams("companyid", shopId + "")
                .tag(this)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                if (!call.isCanceled()) {
                }
            }

            @Override
            public void onResponse(String response, int id) {
                if (response != null) {
                    CollectShopResponse collectShopResponse = JsonUtils.parseByGson(response, CollectShopResponse.class);
                    if (collectShopResponse == null) {
                        return;
                    }
                    if (CallbackCode.SUCCESS.equals(collectShopResponse.getCode())) {
                        ToastUtils.showToast(collectShopResponse.getMs());
                    }else{
                        ToastUtils.showToast(collectShopResponse.getMs());
                    }
                } else {
                }
            }
        });
    }
}
